package com.work.game.ad.impl;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.ATAdRevenueListener;
import com.czhj.sdk.common.utils.Md5Util;
import com.work.game.config.UrlConfig;
import com.work.game.home.ui.enums.RedisEnum;
import com.work.game.home.ui.model.vo.AdRecordSecondaryVo;
import com.work.game.utils.RSAEncrypt;
import com.work.game.volley.httpUtil.HCUParame;
import com.work.game.volley.httpUtil.HUCRequest;
import com.work.game.volley.httpUtil.interfase.HUCCallBackInterfase;
import com.work.game.volley.httpUtil.interfase.HUCParameInterfase;
import com.work.game.volley.httpUtil.interfase.HUCRequestInterfase;
import com.work.game.volley.state.HUCState;

import java.nio.charset.StandardCharsets;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;

public class AdRevenueListenerImpl implements ATAdRevenueListener, HUCCallBackInterfase {
    public static final String TAG = "AdRevenueListenerImpl";

    private String token;
    private String arsPosition;
    private String arsPositionCode;

    private HUCRequestInterfase hucRequestInterfase;
    private HUCParameInterfase hucParameInterfase;

    private Context context;

    private AdRevenueListenerImpl() {
    }

    public AdRevenueListenerImpl(Context context, String token, String arsPositionCode, String arsPosition) {
        this.context = context;
        hucRequestInterfase = HUCRequest.getInstance();
        hucParameInterfase = HCUParame.getInstance();
        this.token = token;
        this.arsPositionCode = arsPositionCode;
        this.arsPosition = arsPosition;
    }

    @Override
    public void onAdRevenuePaid(ATAdInfo atAdInfo) {
        if (StrUtil.isEmpty(token)) {
            return;
        }
        if (ObjectUtil.isNotNull(atAdInfo)) {
            Double ecpm = atAdInfo.getPublisherRevenue(); //展示广告的价格 0.105234661721
            ecpm = ecpm * 10000;
            int firmId = atAdInfo.getNetworkFirmId();  //展示广告的adn名称
            String sdkName = "other";
            switch (firmId) {
                case 28://快手
                    sdkName = "ks";
                    break;
                case 8://腾讯广告(优量汇)
                    sdkName = "gdt";
                    break;
                case 15://穿山甲（CSJ）
                    sdkName = "pangle";
                    break;
                default: //其他
                    break;
            }
            AdRecordSecondaryVo adRecordSecondaryVo = new AdRecordSecondaryVo();
            adRecordSecondaryVo.setArsChannel(sdkName);
            adRecordSecondaryVo.setArsRealityMoney(String.valueOf(ecpm.intValue()));
            adRecordSecondaryVo.setArsMediationRit(atAdInfo.getNetworkPlacementId());
            adRecordSecondaryVo.setToken(token);
            adRecordSecondaryVo.setArsPosition(arsPosition);
            adRecordSecondaryVo.setArsPrimeRit(atAdInfo.getPlacementId());
            adRecordSecondaryVo.setArsPositionCode(arsPositionCode);
            adRecordSecondaryVo.setArsShowId(atAdInfo.getShowId());
            MD5 md5 = MD5.create();
            String name = adRecordSecondaryVo.toString() + RSAEncrypt.publicKey;
            String bytes = md5.digestHex(name);
            adRecordSecondaryVo.setSign(bytes);
            hucRequestInterfase.executeStr(this, null, HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.URL_SECONDARY_ADD, hucParameInterfase.getRequestParameter(adRecordSecondaryVo), true);
        }
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        SharedPreferences sp = context.getSharedPreferences("setting", MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();
        ed.putString(RedisEnum.BIDDING_DEPUTY_GUANG.getKey(), (String) response);//保存token
        ed.commit();
    }

    @Override
    public void onFailStrType(int inputType, Object response) {

    }

    @Override
    public void onSuccessfulBitampType(int inputType, Bitmap response) {

    }

    @Override
    public void onErrorResponse(int inputType, VolleyError response) {

    }

    @Override
    public void onErrorLogin(int inputType, Object response) {

    }

    @Override
    public void onResetLogin(Object response) {

    }

}